package cn.dengta.webapp.trade.entity;

import java.util.Date;
import java.util.Map;
import javax.annotation.Nullable;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.context.model.LabelI18nProxy;
import cn.dengta.context.model.UserEnumI18nKey;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.base.entity.I18nContent;
import cn.dengta.webapp.user.model.Gender;
import com.alibaba.fastjson.annotation.JSONType;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.*;

/**
 * 礼物分类
 *
 * @author lgy
 * @since 2022-03-01
 */
@Setter
@Getter
@JSONType(ignores = { "toggle_", "toggle", "state_", "stateVal", "present" })
@TableName("tr_gift_category")
public class GiftCategory extends BizEntity {

	/** 名称 */
	private String name;
	/** 1:虚拟礼物 2：实体礼物 */
	private Integer type;
	/** 性别 {@link Gender} */
	private Gender gender;
	/** 排序 */
	private Integer sort;

	protected transient I18nContent resolvedLanguageCfg;

	public GiftCategory init(GiftCategory input, Date now) {
		this.name = X.expectNotNull(input.name, name);
		this.type = X.expectNotNull(input.type, type);
		this.gender = X.expectNotNull(input.gender, gender);
		this.sort = X.expectNotNull(input.sort, sort, 0);
		super.initTime(now);
		super.init(input.status, input.state);
		return this;
	}

	@Nullable
	public I18nContent getResolvedLanguageCfg() {
		I18nContent cfg = resolvedLanguageCfg;
		if (cfg == null) {
			resolvedLanguageCfg = cfg = StringUtil.isEmpty(name) ? null : I18nContent.deserialize(name);
		}
		return X.castType(cfg);
	}

	public void updateName(I18nContent config) {
		this.name = I18nContent.serialize(this.resolvedLanguageCfg = config);
	}

	public I18nContent updateResolvedLanguageCfg(String config) {
		this.resolvedLanguageCfg = I18nContent.deserialize(this.name = config);
		return getResolvedLanguageCfg();
	}

	public String getI18nName() {
		return X.map(getResolvedLanguageCfg(), I18nContent::get);
	}

	public static Map<String, Object> asEmbed(GiftCategory source) {
		return asEmbed(source, t -> CollectionUtil.ofHashMap("id", t.getId(), "name", t.getI18nName(),
				"type", t.getType(), "type_", X.map(GiftCategoryType.of(t.getType()), GiftCategoryType::getLabel)));
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Getter
	public enum GiftCategoryType implements LabelI18nProxy<GiftCategoryType, Integer> {

		/** 虚拟礼物 */
		VIRTUAL(1, UserEnumI18nKey.GIFT_CATEGORY_TYPE_VIRTUAL),
		/** 实体礼物 */
		REAL(2, UserEnumI18nKey.GIFT_CATEGORY_TYPE_REAL),
		//
		;

		// 定义私有变量
		public final Integer value;
		final ValueProxy<GiftCategoryType, Integer> proxy;

		// 构造函数，枚举类型只能为私有
		GiftCategoryType(Integer value, String label) {
			this.value = value;
			this.proxy = new ValueProxy<>(this, value, label);
		}

		public static GiftCategoryType[] getValues() {
			return ValueProxy.getCachedArray(GiftCategoryType.class, GiftCategoryType::values);
		}

		public static GiftCategoryType of(Integer value) {
			return VIRTUAL.getValueOf(value);
		}
	}

	public static final String NAME = "name";
	public static final String SORT = "sort";
	public static final String TYPE = "type";
	public static final String GENDER = "gender";

}
